package com.jzo2o.customer.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.service.IBankAccountService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 银行账户 服务实现类
 * </p>
 *
 * @author itlxc
 * @since 2024-10-14
 */
@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {


    /**
     * 新增或修改银行账户信息
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateAccount(BankAccountUpsertReqDTO dto) {
        // 银行账号不能重复
        BankAccount one = this.lambdaQuery().eq(BankAccount::getAccount, dto.getAccount()).one();
        if (ObjectUtil.isNotNull(one)) {
            throw new ForbiddenOperationException("银行账号不能重复");
        }
        BankAccount bankAccount = BeanUtil.copyProperties(dto, BankAccount.class);
        boolean flag = this.saveOrUpdate(bankAccount);
        if (!flag) {
            throw new ForbiddenOperationException("新增银行账户信息失败");
        }
    }

    /**
     * 获取当前用户银行账户信息
     * @return
     */
    @Override
    public BankAccount getCurrentUserBankAccount() {
        Long userId = UserContext.currentUserId();
        if (ObjectUtil.isNull(userId)) {
            throw new ForbiddenOperationException("请登录后再操作");
        }
        return this.lambdaQuery().eq(BankAccount::getId, userId).one();
    }
}
